package com.notes.romania.reminder;

import android.content.SharedPreferences;

public class RemManager {
	public static String PREFS_NAME = "reminders";
	public static String LIST = "list";
	
	public static SharedPreferences settings;
	public static SharedPreferences.Editor editor;
	
	public static void setUp(SharedPreferences s, SharedPreferences.Editor e){
    	settings = s;
		editor = e;
	}
	
	public static void setNrOfRem(int nr){
		editor.putInt("nrOfRem", nr);
		editor.commit();
	}
	public static int getNrOfRem(){
		return settings.getInt("nrOfRem", 0);
	}
	
	public static void setListSize(int nr){
		editor.putInt(LIST + "size", nr);
		editor.commit();
	}
	public static int getListSize(){
		return settings.getInt(LIST + "size", 0);
	}
	
	public static void putIdToList(int id){
		setListSize(getListSize() + 1);
		
		editor.putInt(LIST + getListSize(), id);
		editor.commit();
	}
	private static boolean removeIdFromList(int id){
		int listSize = getListSize(), i;
		for(i = 1; i <= listSize; i ++)
			if(settings.getInt(LIST + i, 0) == id)
				break;
		if(i > listSize)
			return false;	//Not Found
				
		//We put the last element on the i'th position and delete it
		editor.putInt(LIST + i, settings.getInt(LIST + listSize, 0));
		editor.remove(LIST + listSize);
		editor.commit();
		setListSize(getListSize() - 1);
		
		return true;	//Found
	}
	
	public static int getIdAtIndex(int index){
		return settings.getInt(LIST + index, 0);
	}
	
	public static Reminder readRem(int id){
		Reminder ret = new Reminder();
		
		ret.setId(id);
		
		ret.setDateTime(
				settings.getInt(getCustomKey(id, "year"), 0),
				settings.getInt(getCustomKey(id, "month"), 0),
				settings.getInt(getCustomKey(id, "day"), 0),
				settings.getInt(getCustomKey(id, "hour"), 0),
				settings.getInt(getCustomKey(id, "minute"), 0));
		
		ret.setRepeatValues(
				settings.getInt(getCustomKey(id, "yearsDelay"), 0),
				settings.getInt(getCustomKey(id, "daysDelay"), 0),
				settings.getInt(getCustomKey(id, "hoursDelay"), 0),
				settings.getInt(getCustomKey(id, "minutesDelay"), 0));
		
		ret.setRepeat(settings.getBoolean(getCustomKey(id, "repeat"), false));
		
		ret.setText(settings.getString(getCustomKey(id, "text"), ""));
		
		ret.setDefaultSound(settings.getBoolean(getCustomKey(id, "defaultSound"), true));
		ret.setSoundPath(settings.getString(getCustomKey(id, "soundPath"), ""));
		
		return ret;
	}
	
	public static void putRem(Reminder rem){
		putIdToList(rem.id);
		
		editor.putInt(getCustomKey(rem.id, "id"), rem.id);
		
		editor.putInt(getCustomKey(rem.id, "year"), rem.year);
		editor.putInt(getCustomKey(rem.id, "month"), rem.month);
		editor.putInt(getCustomKey(rem.id, "day"), rem.day);
		editor.putInt(getCustomKey(rem.id, "hour"), rem.hour);
		editor.putInt(getCustomKey(rem.id, "minute"), rem.minute);
		
		editor.putInt(getCustomKey(rem.id, "yearsDelay"), rem.yearsDelay);
		editor.putInt(getCustomKey(rem.id, "daysDelay"), rem.daysDelay);
		editor.putInt(getCustomKey(rem.id, "hoursDelay"), rem.hoursDelay);
		editor.putInt(getCustomKey(rem.id, "minutesDelay"), rem.minutesDelay);
		
		editor.putBoolean(getCustomKey(rem.id, "repeat"), rem.repeat);
		
		editor.putString(getCustomKey(rem.id, "text"), rem.text);
		
		editor.putBoolean(getCustomKey(rem.id, "defaultSound"), rem.defaultSound);
		editor.putString(getCustomKey(rem.id, "soundPath"), rem.soundPath);
		
		editor.commit();
	}
	
	public static void removeRem(Reminder rem){
		removeIdFromList(rem.id);
		
		editor.remove(getCustomKey(rem.id, "id"));
		
		editor.remove(getCustomKey(rem.id, "year"));
		editor.remove(getCustomKey(rem.id, "month"));
		editor.remove(getCustomKey(rem.id, "day"));
		editor.remove(getCustomKey(rem.id, "minute"));
		
		editor.remove(getCustomKey(rem.id, "yearsDelay"));
		editor.remove(getCustomKey(rem.id, "daysDelay"));
		editor.remove(getCustomKey(rem.id, "hoursDelay"));
		editor.remove(getCustomKey(rem.id, "minutesDelay"));

		editor.remove(getCustomKey(rem.id, "repeat"));
		
		editor.remove(getCustomKey(rem.id, "text"));
		
		editor.remove(getCustomKey(rem.id, "defaultSound"));
		editor.remove(getCustomKey(rem.id, "soundPath"));
		
		editor.commit();
		
	}
	public static String getKey(int id){
		return id + "";
	}
	public static String getCustomKey(int id, String a){
		return getKey(id) + a;
	}
}
